package fragment;

import java.util.Vector;

public class Reservation extends QuasiComponentReservation{

	private final Vector<String> requiredElements;
	private Vector<String> reservedElements=new Vector<String>();
	
	public Reservation(Vector<String> requiredElements) {
		super();
		this.requiredElements = requiredElements;
	}

	@Override
	public void reserve(String mmme) {
		synchronized(reservedElements){
			if(!reservedElements.contains(mmme)){
				reservedElements.add(mmme);
				this.architecture().informOfReservation(mmme);
			}
		}
	}
	
	@Override
	public void release(String mmme) {
		synchronized(reservedElements){
			if(reservedElements.contains(mmme)){
				reservedElements.remove(mmme);
				this.architecture().informOfRelease(mmme);
			}
		}
	}
	
	@Override
	public void releaseAll() {
		synchronized(reservedElements){
			for(int i=0;i<reservedElements.size();i++)
				this.architecture().informOfRelease(reservedElements.get(i));
			reservedElements.clear();
		}
	}
	
	@Override
	public Vector<String> requiredElements() {
		synchronized(reservedElements){
			return requiredElements;
		}
	}
	
	@Override
	public boolean allNeededElementReserved() {
		
		synchronized(reservedElements){
			boolean res=true;
			for(String s : requiredElements)
				if 	(! reservedElements.contains(s)) res=false;
			return res;
		}

	}
	
	@Override
	public void start() {
			
	}

	



}
